package com.zf.yichat.config.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import com.zf.yichat.dto.response.IPV6Dto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * IPV6转地区
 */
@Component
public class IPV6ToRegionConverter {


    @Value("${st.baiduyun.apiUri}")
    private String IPV_API_URI;
    @Value("${st.baiduyun.AccessKey}")
    private String IPV_ACCESS_KEY;
    @Value("${st.baiduyun.AppCode}")
    private String IPV_APP_CODE;
    @Value("${st.baiduyun.AppSecret}")
    private String IPV_APP_SECRET;


    public IPV6Dto queryAreaByIPV6(String ip){

        String path = IPV_API_URI;
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.GET, path);
        request.setCredentials(IPV_ACCESS_KEY, IPV_APP_SECRET);
        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/json; charset=utf-8");
        // 设置query参数
        request.addQueryParameter("ip", ip);
        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
            ApiExplorerResponse response = client.sendRequest(request);
            String result = response.getResult();
            IPV6Dto ipv6Dto = JSON.parseObject(result, IPV6Dto.class);
            return ipv6Dto;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new IPV6Dto();
    }

    public static boolean isIPv4Address (String address) {
        try {
            InetAddress inetAddress = InetAddress.getByName (address);
            return inetAddress.getAddress ().length == 4;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isIPv6Address (String address) {
        try {
            InetAddress inetAddress = InetAddress.getByName (address);
            return inetAddress.getAddress ().length == 16;
        } catch (Exception e) {
            return false;
        }
    }
    public static void main (String [] args) {

        String path = "https://gwgp-unbrggj3dfn.n.bdcloudapi.com/v2/ip/ipv6/query";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.GET, path);
        request.setCredentials("42dd4220af714af6b2ffc0901dbb267e", "e5bac47dcc0c4dce84fdc7214e3f6b61");
        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/json; charset=utf-8");
        // 设置query参数
        request.addQueryParameter("ip", "2408:843e:7e80:29fc:1766:8bee:4752:f0f9");
        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
            ApiExplorerResponse response = client.sendRequest(request);
            String result = response.getResult();
            JSONObject jsonObject = JSON.parseObject(result);
            String success = jsonObject.getString("success");

            // 返回结果格式为Json字符串
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
